package com.ljc.dao;

import com.ljc.dto.DeviceDistribute;
import com.ljc.dto.DeviceSta;
import com.ljc.dto.DeviceStatAge;
import com.ljc.dto.Devices;
import com.ljc.entity.DevicePurchaseDemand;
import com.ljc.vo.DeviceSearch;
import com.ljc.vo.StaOrg;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
@Mapper
public interface DevicePurchaseDemandDao {
    @Insert("insert into  " +
            "device_purchase_demand(name,quantity,technical_parameter,supplier,price,phone,delivery_date,purchase_apply_id)" +
            "values(#{name},#{quantity},#{technicalParameter},#{supplier},#{price},#{phone},#{deliveryDate},#{purchaseApplyId})")
    void insert(DevicePurchaseDemand devicePurchaseDemand);

    @Select("select * from device_purchase_demand where purchase_apply_id=#{purchaseApplyId}")
    List<DevicePurchaseDemand> findByPurchaseApply(int purchaseApplyId);

    List<Devices> searchByAdmit(DeviceSearch deviceSearch);

    List<DeviceDistribute> statTotalByAdmit(DeviceSearch deviceSearch);

    List<String> searchAllName(int admitId);
    int countApply(int admitId);
    int countName(int admitId);
    int countQuantity(int admitId);
    int countPrice(int admitId);
    List<String> searchAllOrganization(int admitId);
    List<DeviceStatAge> statAgeByAdmit(DeviceSearch deviceSearch);


    List<Devices> searchByUser(DeviceSearch deviceSearch);

    List<DeviceSta> deviceSta(DeviceSearch deviceSearch);

    @Select("select SUM(quantity)quantity from device_purchase_demand where\n" +
            "purchase_apply_id in\n" +
            "(SELECT id from purchase_apply where  status=3 and user_id=#{userId})\n")
    int countQuantityByUser(int UserId);

    @Select("select SUM(quantity*price)price from device_purchase_demand where" +
            " purchase_apply_id in (select id from purchase_apply where status=3 and user_id=#{userId})")
    int countPriceByUser(int userId);

    @Select("   select DISTINCT(name) from device_purchase_demand where\n" +
            "purchase_apply_id in\n" +
            "(SELECT id from purchase_apply where  status=3 and user_id=#{userId})")
    List<String> searchNameByUser(int userId);

    List<DeviceStatAge> deviceAgeStaByUser(DeviceSearch deviceSearch);

    List<DeviceSta> staByOrg(StaOrg staOrg);
}
